using System;
using System.Diagnostics;
using System.Linq.Expressions;
using MongoDB.Bson.Serialization;

namespace Charisma.MongoDb.Linq.Expressions
{
  [DebuggerDisplay("{Name}")]
  internal sealed class FieldExpression : MongoExpression
  {
    public string Name { get; private set; }
    public BsonClassMap MetaType { get; private set; }
    public Expression Expression { get; private set; }

    public override bool CanReduce
    {
      get { return true; }
    }

    public override Expression Reduce()
    {
      return Expression;
    }

    protected override Expression Accept(ExpressionVisitor visitor)
    {
      var mongoVisitor = visitor as MongoExpressionVisitor;
      if (mongoVisitor == null)
        return this;
      return mongoVisitor.VisitField(this);
    }

    public override string ToString()
    {
      return Name;
    }

    public FieldExpression(Type type, string name, BsonClassMap metaType, Expression expression)
      : base(MongoExpressionType.Field, type)
    {
      Name = name;
      MetaType = metaType;
      Expression = expression;
    }
  }
}